package com.example.exercise.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.List;

import com.example.exercise.demo.vo.AppPapersVO;
import com.example.exercise.demo.service.AppPapersService;
import com.example.exercise.common.base.BaseResponse;

/**
* 
*/
@RestController
@RequestMapping("/appPapers")
@Tag(name = "", description = "")
public class AppPapersController {

    @Autowired
    private AppPapersService appPapersService;

    @Operation(summary = "分页查询列表", description = "分页查询列表")
    @RequestMapping(value = "findByWhere", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE})
    public BaseResponse<Page<AppPapersVO>> findByWhere(@RequestBody AppPapersVO appPapersVO, Page<AppPapersVO> page){
        BaseResponse<Page<AppPapersVO>> baseResponse = new BaseResponse<>();
        List<AppPapersVO> list = appPapersService.findByWhere(page, appPapersVO);
        page.setRecords(list);
        baseResponse.ok(page);
        return baseResponse;
    }

    @Operation(summary = "根据id查详情", description = "根据id查详情")
    @RequestMapping(value = "findByKey", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<AppPapersVO> findByKey(@RequestBody AppPapersVO appPapersVO){
        BaseResponse<AppPapersVO> baseResponse = new BaseResponse<>();
        AppPapersVO appPapersVO1 = appPapersService.findByKey(appPapersVO);
        if(appPapersVO1 == null){
            baseResponse.errMsg("数据为空");
        }else{
            baseResponse.ok(appPapersVO1);
        }
        return baseResponse;
    }

    @Operation(summary = "添加一条记录", description = "添加一条记录")
    @RequestMapping(value = "insertAppPapers", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<Object> insertAppPapers(@RequestBody AppPapersVO appPapersVO){
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        int count = appPapersService.insert(appPapersVO);
        if(count > 0){
            baseResponse.ok();
        }else{
            baseResponse.errMsg("新增数据失败");
        }
        return baseResponse;
    }

    @Operation(summary = "修改一条记录", description = "修改一条记录")
    @RequestMapping(value = "updateAppPapersByKey", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<Object> updateAppPapersByKey(@RequestBody AppPapersVO appPapersVO){
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        int count = appPapersService.updateByKey(appPapersVO);
        if(count > 0){
            baseResponse.ok();
        }else{
            baseResponse.errMsg("修改数据失败");
        }
        return baseResponse;
    }

    @Operation(summary = "删除一条记录", description = "删除一条记录")
    @RequestMapping(value = "deleteByKey", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE })
    public BaseResponse<Object> deleteByKey(@RequestBody AppPapersVO appPapersVO){
        BaseResponse<Object> baseResponse = new BaseResponse<>();
        int count = appPapersService.deleteByKey(appPapersVO);
        if(count > 0){
            baseResponse.ok();
        }else{
            baseResponse.errMsg("删除数据失败");
        }
        return baseResponse;
    }

}